package com.boarsoft.boar.common;

import java.util.regex.Pattern;

import com.boarsoft.common.Util;

public class ValidateUtil {
	public static final String REGEXP_USERNAME = "^[a-z0-9_-]{3,16}$"; // 用户名
	public static final String REGEXP_PASSWORD = "^[a-z0-9_-]{6,18}$"; // 密码
	// public static final String REGEXP_HEX =
	// "^#?([a-f0-9]{6}|[a-f0-9]{3})$";// 十六进制值
	public static final String REGEXP_EMAIL = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"; // 电子邮箱
	// public static final String REGEXP_EMAIL =
	// "^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$"
	public static final String REGEXP_PHONE = "^(13[0-9]|14[0-9]|15[0-9]|170|177|18[0-9])\\d{8}$";//

	public static final String REGEXP_URL = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"; //
	public static final String REGEXP_IPV4 = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; //
	public static final String REGEXP_IPV6 = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; //
	public static final String REGEXP_HTML = "^<([a-z]+)([^<]+)*(?:>(.*)<\\/\\1>|\\s+\\/>)$"; // 标签
	// public static final String REGEXP_ = "(?<!http:|\S)//.*$"; // 删除代码\\注释
	public static final String REGEXP_CHINESE = "^[\u2E80-\u9FFF]+$";// Unicode编码中的汉字范围

	public static boolean isValidEmail(String s) {
		return matchRegExp(s, REGEXP_EMAIL);
	}

	public static boolean isValidPhone(String s) {
		return matchRegExp(s, REGEXP_PHONE);
	}

	/**
	 * 检查字符串是否匹配某个正则表达式
	 * 
	 * @param str
	 *            字符串
	 * @param regex
	 *            正则表达式
	 * @return
	 */
	public static boolean matchRegExp(String str, String regex) {
		if (Util.strIsEmpty(str))
			return false;
		return Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(str).matches();
	}
}